package com.example.doer.reading.AppActivities;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.view.titlebar.AbTitleBar;
import com.example.doer.reading.AppDataModel.AppUser;
import com.example.doer.reading.R;
import com.example.doer.reading.global.MyApplication;

import static com.ab.util.AbToastUtil.showToast;

public class RegistPassActivity extends AbActivity {
    MyApplication application;
    private String userPhone;
    private EditText setName;
    private EditText setJob;
    private EditText setSchool;
    private EditText setDescription;
    private Spinner sexSpinner;
    private TextView sexText;

    private String sex;
    private String name;
    private String job;
    private String school;
    private String description;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.activity_regist_pass);
        AbTitleBar abTitleBar=RegistPassActivity.this.getTitleBar();
        abTitleBar.setTitleBarBackgroundColor(getResources().getColor(R.color.colorPrimary));
        abTitleBar.setLogo(R.mipmap.return_button);
        abTitleBar.setVisibility(View.VISIBLE);
        abTitleBar.setTitleText("个人信息注册");
        application=(MyApplication)abApplication;
        setName =(EditText)findViewById(R.id.SetName);
        setJob=(EditText)findViewById(R.id.SetJob);
        setSchool=(EditText)findViewById(R.id.SetSchool);
        setDescription=(EditText)findViewById(R.id.SetDescription);
        sexSpinner= (Spinner) findViewById(R.id.spinner);
        sexText= (TextView) findViewById(R.id.sexText);
        sex=sexText.getText().toString();
        Button setpass=(Button)findViewById(R.id.SetPassBtn);
        userPhone=getIntent().getStringExtra("userPhone");

        sexSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String[] sexes=getResources().getStringArray(R.array.sexes);
                sexText.setText(sexes[i]);
                sex=sexText.getText().toString();
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });


        setpass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                name=setName.getText().toString();
                job=setJob.getText().toString();
                school=setSchool.getText().toString();
                description=setDescription.getText().toString();
                if (name.equals("")){
                    showToast(RegistPassActivity.this,"昵称不能为空");
                    return;}
                else if (job.equals(""))
                    {
                    showToast(RegistPassActivity.this,"职业不能为空");
                    return;
                    }
                else if (school.equals(""))
                    {showToast(RegistPassActivity.this,"学校不能为空");
                    return;
                    }
                else if (description.equals(""))
                    {showToast(RegistPassActivity.this,"个人简介不能为空");
                    return;}
                else if(name!=null&&job!=null&&school!=null&&description!=null){
                    Intent intent=new Intent(RegistPassActivity.this,test2Activity.class);
                    AppUser user=new AppUser();
                    user.setUserName(name);
                    user.setUserID(userPhone);
                    user.setUserSchool(school);
                    user.setUserJob(job);
                    user.setUserDescription(description);
                    user.setUserSex(sex);
                    intent.putExtra("user",user);
                     startActivity(intent);
                    }
            }
        });
    }
//    public void UpdateUserToServer(){
////        final String url="http://192.168.191.1/Reading/InfoDoServlet?UserID="+user.getUserID()+"&UserPass="+user.getUserPassWord();
//        String url="http://192.168.191.1/Reading/InfoDoServlet?Action=register&userID="+userPhone+"&userName="+name+
//                "&userJob="+job+"&userSchool="+school+"&description="+description+"&sex="+sex;
//        AbHttpUtil abHttpUtil=AbHttpUtil.getInstance(RegistPassActivity.this);
//        abHttpUtil.get(url, new AbStringHttpResponseListener() {
//            @Override
//            public void onSuccess(int i, String s) {
//                if (s.equals("true")){
//                    showToast(RegistPassActivity.this,"注册成功");
//                    AppUser user=new AppUser();
//                    user.setUserID(userPhone);
//                    user.setUserName(name);
//                    user.setLoginUser(true);
//                    application.updateLoginParams(user);
//                    //上传数据库
//                    application.setFirstStart(false);
//                    startActivity(new Intent(RegistPassActivity.this,testActivity.class));
//                    RegistPassActivity.this.finish();
//                }else
//                    showToast(RegistPassActivity.this,"注册失败,是否已有账户,去登录？");
//                //不能写在完成中，因为完成在有重定向的情况会提前结束
//                AbDialogUtil.removeDialog(RegistPassActivity.this);
//            }
//
//            @Override
//            public void onStart() {
//                AbDialogUtil.showProgressDialog(RegistPassActivity.this,0,"正在执行...");
//            }
//
//            @Override
//            public void onFinish() {
//
//            }
//
//            @Override
//            public void onFailure(int i, String s, Throwable throwable) {
//                AbDialogUtil.removeDialog(RegistPassActivity.this);
//                AbToastUtil.showToast(RegistPassActivity.this,throwable.getMessage());
//            }
//        });
//    }


}
